package ink.tiptop.controller;

import ink.tiptop.persist.domain.entity.ZjfBaseServerInfoEntity;
import ink.tiptop.dto.req.base.server.*;
import ink.tiptop.enums.EResp;
import ink.tiptop.common.domain.WebworkResponse;
import ink.tiptop.common.domain.rsp.Response;
import ink.tiptop.service.ZjfBaseServerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import javax.validation.Valid;
import ink.tiptop.utils.JSON;
/**
 * 服务器基本连接信息(ZjfBaseServerInfoEntity)表控制层
 *
 * @author makejava
 * @since 2020-06-21 20:40:28
 */
@Api("服务器基本信息管理API")
@RestController
@RequestMapping("zjfBaseServerInfo")
@Slf4j
public class ServerController {
    /**
     * 服务对象
     */
    @Resource
    private ZjfBaseServerInfoService zjfBaseServerInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("查询单一服务器信息")
    @GetMapping("selectOne")
    public ZjfBaseServerInfoEntity selectOne(Integer id) {
        return this.zjfBaseServerInfoService.queryById(id);
    }



    @ApiOperation("服务器基本信息添加")
    @PostMapping("add")
    @ResponseBody
    public Response add(@RequestBody @Valid AddZjfBaseServerInfoRequest request) {
        log.info("服务器基本信息添加:{}", JSON.toJSONString(request));
        if (request.getConnWay().equals(0) && StringUtils.isBlank(request.getPass())) {
            return WebworkResponse.failed(EResp.EServerInfo.ERROR_PASS_EXIST);
        }
        if (request.getConnWay().equals(1) && StringUtils.isBlank(request.getKey())) {
            return WebworkResponse.failed(EResp.EServerInfo.ERROR_PUBKEY_EXIST);
        }
        return zjfBaseServerInfoService.add(request);
    }

    @ApiOperation("查询服务器基本信息")
    @PostMapping("list")
    @ResponseBody
    public Response list(@RequestBody QueryZjfBaseServerInfoRequest request) {
        log.info("查询服务器基本信息,入参:{}", JSON.toJSONString(request));
        // 查询服务器基本信息返回 数据
        return this.zjfBaseServerInfoService.list(request);
    }

    @ApiOperation("批量删除服务器基本信息")
    @PostMapping("delete")
    @ResponseBody
    public Response delete(@RequestBody @Valid DeleteZjfBaseServerInfoRequest request) {
        log.info("批量删除服务器基本信息,入参:{}", JSON.toJSONString(request));
        // 查询服务器基本信息返回 数据
        return this.zjfBaseServerInfoService.delete(request);
    }

    @ApiOperation("更新服务器基本信息")
    @PostMapping("update")
    @ResponseBody
    public Response update(@RequestBody @Valid UpdateZjfBaseServerInfoRequest request) {
        log.info("更新服务器基本信息,入参:{}", JSON.toJSONString(request));
        // 查询服务器基本信息返回 数据
        return this.zjfBaseServerInfoService.update(request);
    }

    @ApiOperation("导出数据")
    @PostMapping("export")
    @ResponseBody
    public Response update(@RequestBody @Valid ExportRequest request) {
        log.info("导出服务器基本信息,入参:{}", JSON.toJSONString(request));
//        return this.zjfBaseServerInfoService.export(request);
        return null;
    }

}